package com.yygo.service;

import com.yygo.configentity.action.Phase;
import com.yygo.configentity.action.Role;
import com.yygo.configentity.action.RoleActions;
import com.yygo.model.User;
import com.yygo.util.RoleActionConfigUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by huanli on 2015/11/14.
 */
public class RoleActionsConfigurer {

    private RoleActions roleActions;

    public Phase getRoleAction(String role, String phase) {
        List<Role> roleList = roleActions.getRoles().stream().filter((actionRole)-> actionRole.getName().equals(role)).collect(Collectors.toList());
        if(roleList.size()>0) {
            List<Phase> retActions = roleList.get(0).getRoleActions().stream().filter((actionPhase) -> actionPhase.getName().equals(phase)).collect(Collectors.toList());
            if(retActions.size()>0) return retActions.get(0);
        }
        return null;
    }

    public boolean isGrab(String role, String phase) {
        Phase rolePhase = getRoleAction(role, phase);
        if (rolePhase != null)
            return rolePhase.isGrab();
        return false;
    }

    public void init() {
        roleActions = new RoleActionConfigUtil().read();
    }
}
